﻿using PEModels;
using PerformanceExamine.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PerformanceExamine
{
    public partial class FormSetUser : Form
    {
        private DeleBindDgv _deleBindDgv;
        public FormSetUser(DeleBindDgv deleBindDgv)
        {
            InitializeComponent();
            _deleBindDgv = deleBindDgv;
        }

        private Users _user;
        public FormSetUser(DeleBindDgv deleBindDgv,int userId):this(deleBindDgv)
        {
            _user =  Users.GetListUsers().Find(m=>m.Id==userId);
        }

        private void FormSetUser_Load(object sender, EventArgs e)
        {
            bindcbxIdentity();
            if (_user != null)
            {
                txtUserName.Text = _user.UserName;
                cbxIdentity.SelectedValue = _user.BaseTypeId;
                cbxSex.Text = _user.Sex;
                chkIsStop.Checked = _user.IsDel;
            }    
        }

        private void bindcbxIdentity()
        {
            List<AppraisalBases> appraisalBases = new List<AppraisalBases>();
            appraisalBases = AppraisalBases.ListAll();
            cbxIdentity.DataSource = appraisalBases;
            cbxIdentity.DisplayMember = "BaseType";
            cbxIdentity.ValueMember = "Id";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            String userName = txtUserName.Text.Trim();
            int BaseTypeId = (int)cbxIdentity.SelectedValue;
            string sex = cbxSex.Text;
            bool isDel = chkIsStop.Checked;            
            if (_user==null)
            {
                Users user = new Users()
                {
                    UserName = userName,
                    BaseTypeId = BaseTypeId,
                    Password = "111",
                    Sex = sex,
                    IsDel = isDel
                };
                Users.Insert(user);
                MessageBox.Show("用户添加成功");
            }
            else
            {
                _user.UserName = userName;
                _user.BaseTypeId = BaseTypeId;
                _user.Sex = sex;
                _user.IsDel = isDel;
                Users.Update(_user);
                MessageBox.Show("用户修改成功");
            }
            _deleBindDgv();
            this.Close();
        }
    }
}
